package com.slz3.business.module.memberManagement.userScoreRecord.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.memberManagement.userScoreRecord.domain.UserScoreRecord;
import com.slz3.business.module.memberManagement.userScoreRecord.domain.UserScoreRecordFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

public class UserScoreRecordResExecutor {

    protected Mono<ServerResponse> add(ServerRequest req, UserScoreRecord userScoreRecord) {
        UserScoreRecord userScoreRecordDBLatest = Services.userScoreRecord.save(userScoreRecord);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(userScoreRecordDBLatest).build());
    }

    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> delete(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {
            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecord.delete(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> modify(ServerRequest req, UserScoreRecord source) {
        UserScoreRecord target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            UserScoreRecord userScoreRecordDBLatest = Services.userScoreRecord.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(userScoreRecordDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> list(ServerRequest req) {
        Page<UserScoreRecord> userScoreRecordPage = Services.userScoreRecord.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(userScoreRecordPage).build());
    }

    protected Mono<ServerResponse> info(ServerRequest req) {
        UserScoreRecord target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> choice(ServerRequest req) {
        Page<UserScoreRecord> userScoreRecordPage = Services.userScoreRecord.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(userScoreRecordPage).build());
    }

    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {

            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> audit(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {

            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> auditReject(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {

            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {

            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: userScoreRecordList is not null
     */
    protected Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<UserScoreRecord> userScoreRecordList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(userScoreRecordList)) {

            List<UserScoreRecord> userScoreRecordListNew = Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
            if (VE.isNotEmpty(userScoreRecordListNew) && userScoreRecordListNew.size() == userScoreRecordList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(userScoreRecordListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
